home *** CD-ROM | disk | FTP | other *** search
/ Aminet 44 / Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso / Aminet / misc / emu / p-interp.lha / p-interp-0.4 / native6502.c < prev    next >
C/C++ Source or Header  |  2001-06-06  |  6KB  |  215 lines

  1. /*
  2.  
  3.   P-Code interpreter (to run the apple pascal system)
  4.   Copyright (C) 2000 Mario Klebsch
  5.  
  6.   This program is free software; you can redistribute it and/or modify
  7.   it under the terms of the GNU General Public License as published by
  8.   the Free Software Foundation; either version 2 of the License, or
  9.   (at your option) any later version.
  10.  
  11.   This program is distributed in the hope that it will be useful,
  12.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.   GNU General Public License for more details.
  15.  
  16.   You should have received a copy of the GNU General Public License
  17.   along with this program; if not, write to the Free Software
  18.   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20.  
  21.   $Log: native6502.c,v $
  22.   Revision 1.4  2001/06/06 23:14:19  mario
  23.   Turtlegraphics wird jetzt mit einem #define aktiviert
  24.  
  25.   Revision 1.3  2001/05/26 15:13:29  mario
  26.   Diverse kleine Fehler behoben, fehlende #includes, Labels ohne Statement
  27.   dahinter, ...
  28.  
  29.   Revision 1.2  2001/05/20 13:12:02  mario
  30.   CVS-Idents und Logs eingefügt
  31.  
  32.  
  33. */
  34.  
  35. #ident "$Id: native6502.c,v 1.4 2001/06/06 23:14:19 mario Exp $";
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <time.h>
  40.  
  41. #include "psystem.h"
  42. #include "pcode.h"
  43. #include "Memory.h"
  44. #include "Array.h"
  45. #include "Term.h"
  46.  
  47. static void IdSearch(word EntryPoint)
  48. {
  49.   word    BufPtr    = Pop();
  50.   word    Arg2Ptr   = Pop();
  51.  
  52.   CspIdSearch(BufPtr, Arg2Ptr);
  53. }
  54.  
  55. static byte IdSearchSig[]=
  56. { 0x68, 0x85, 0x7e, 0x68, 0x85, 0x7f, 0x68, 0xa8, 0x68, 0xaa,
  57.   0x68, 0x85, 0x94, 0x68, 0x85, 0x95, 0x98, 0xa0, 0x00, 0x18,
  58.   0x71, 0x94, 0x85, 0x96, 0x8a, 0xc8, 0x71, 0x94, 0x85, 0x97,
  59.   0xa9, 0x20, 0xa2, 0x07, 0x95, 0x88, 0xca, 0xd0, 0xfb, 0x88,
  60.   0xb1, 0x96, 0xc9, 0x61, 0x90, 0x07, 0xc9, 0x7b, 0xb0, 0x03 };
  61.  
  62. static void TreeSearch(word EntryPoint)
  63. {
  64.   word    TokenBuf;
  65.   word    ResultPtr;
  66.   word    NodePtr;
  67.  
  68.   Pop(); Pop();
  69.   TokenBuf  = Pop();
  70.   ResultPtr = Pop();
  71.   NodePtr   = Pop();            /* initialize with root node addr   */
  72.  
  73.   Push(CspTreeSearch(TokenBuf, ResultPtr, NodePtr));
  74. }
  75.  
  76. static void Randomize(word EntryPoint)
  77. {
  78.   srand(time(0L));
  79. }
  80.  
  81. static byte RandomizeSig1[]=
  82. { 0xad, 0x13, 0xbf, 0x8d, 0x3f, 0x02, 0x8d, 0x40,
  83.   0x02, 0xad, 0x14, 0xbf, 0x8d, 0x41, 0x02, 0x8d,
  84.   0x42, 0x02, 0x0d, 0x3f, 0x02, 0xd0, 0x05, 0xa9,
  85.   0x55, 0x8d, 0x42, 0x02, 0x60, 0x00, 0x00, 0x00,
  86.   0x00, 0x00, 0x1e, 0x00, 0x1d, 0x00, 0x19, 0x00,
  87.   0x18, 0x00, 0x17, 0x00, 0x12, 0x00, 0x06, 0x00,
  88.   0x00, 0x00, 0x32, 0x00 };
  89.  
  90. static byte RandomizeSig2[]=
  91. { 0xad, 0x13, 0xbf, 0x8d, 0x25, 0x02, 0x8d, 0x26,
  92.   0x02, 0xad, 0x14, 0xbf, 0x8d, 0x27, 0x02, 0x8d,
  93.   0x28, 0x02, 0x0d, 0x25, 0x02, 0xd0, 0x05, 0xa9,
  94.   0x55, 0x8d, 0x28, 0x02, 0x60, 0x00, 0x00, 0x00,
  95.   0x00, 0x00, 0x1e, 0x00, 0x1d, 0x00, 0x19, 0x00,
  96.   0x18, 0x00, 0x17, 0x00, 0x12, 0x00, 0x06, 0x00,
  97.   0x00, 0x00, 0x32, 0x00 };
  98.  
  99. static void Keypress(word EntryPoint)
  100. {
  101.   Pop();
  102.   Pop();
  103.   Push(TermStat());
  104. }
  105.  
  106. static byte KeypressSig[]=
  107. {  0x68, 0x85, 0x00, 0x68, 0x85, 0x01, 0x68, 0x68,
  108.    0x68, 0x68, 0xa9, 0x00, 0x48, 0xad, 0x21, 0xbf,
  109.    0xd0, 0x06, 0x20, 0x5c, 0xff, 0x4c, 0x1b, 0x00,
  110.    0x20, 0x0a, 0xbf, 0xad, 0x18, 0xbf, 0xcd, 0x19,
  111.    0xbf, 0xf0, 0x04, 0xa9, 0x01, 0xd0, 0x02, 0xa9,
  112.    0x00, 0x48, 0xa5, 0x01, 0x48, 0xa5, 0x00, 0x48,
  113.    0x60, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x01, 0x00,
  114.    0x00, 0x00, 0x00, 0x00, 0x3c, 0x00 };
  115.  
  116. static void Random(word EntryPoint)
  117. {
  118.   Pop();
  119.   Pop();
  120.   Push(rand()&0x7fff);
  121. }
  122.  
  123. static byte RandomSig[]=
  124. {  0x68, 0x85, 0x00, 0x68, 0x85, 0x01, 0x68, 0x68,
  125.    0x68, 0x68, 0xa0, 0x07, 0x0e, 0x39, 0x00, 0x08,
  126.    0x2e, 0x3a, 0x00, 0x2e, 0x3b, 0x00, 0x2e, 0x3c,
  127.    0x00, 0x30, 0x05, 0x28, 0x10, 0x08, 0x30, 0x03,
  128.    0x28, 0x30, 0x03, 0xee, 0x39, 0x00, 0x88, 0xd0,
  129.    0xe3, 0xad, 0x39, 0x00, 0x4a, 0x48, 0xad, 0x3b,
  130.    0x00, 0x48, 0xa5, 0x01, 0x48, 0xa5, 0x00, 0x48,
  131.    0x60, 0x5a, 0xb2, 0xf6, 0x93, 0x00, 0x00, 0x00,
  132.    0x33, 0x00, 0x31, 0x00, 0x30, 0x00, 0x2f, 0x00,
  133.    0x24, 0x00, 0x20, 0x00, 0x1d, 0x00, 0x07, 0x00,
  134.    0x00, 0x00, 0x00, 0x00, 0x54, 0x00 };
  135.  
  136. static byte TreeSearchSig[]=
  137. { 0x68, 0x85, 0x84, 0x68, 0x85, 0x85, 0x68, 0x68, 0x68, 0x68,
  138.   0x68, 0x85, 0x82, 0x68, 0x85, 0x83, 0x68, 0x85, 0x7e, 0x68,
  139.   0x85, 0x7f, 0x68, 0x85, 0x80, 0x68, 0x85, 0x81, 0xa2, 0x08,
  140.   0xa0, 0xff, 0xca, 0x30, 0x37, 0xc8, 0xb1, 0x82, 0xd1, 0x80,
  141.   0xf0, 0xf6, 0x30, 0x17, 0xa0, 0x08, 0xb1, 0x80, 0xaa, 0xc8,
  142.   0xb1, 0x80, 0xc9, 0x00, 0xd0, 0x04, 0xe0, 0x00, 0xf0, 0x2c,
  143.   0x86, 0x80, 0x85, 0x81, 0x4c, 0x1c };
  144.  
  145. #ifdef TURTLEGRAPHICS
  146. extern void TurtleGraphics(word EntryPoint);
  147. static byte TurtleSig[16]="TURTLEGRAPHICS::";
  148. #endif
  149.  
  150. struct
  151. {
  152.   byte    *Sig;
  153.   int    SigLen;
  154.   void    (*Proc)(word);
  155. } Routines[]={ { IdSearchSig, sizeof(IdSearchSig),    IdSearch},
  156.            { TreeSearchSig, sizeof(TreeSearchSig),    TreeSearch},
  157. #ifdef TURTLEGRAPHICS
  158.            { TurtleSig, sizeof(TurtleSig),        TurtleGraphics},
  159. #endif
  160.            { RandomizeSig1, sizeof(RandomizeSig1),    Randomize},
  161.            { RandomizeSig2, sizeof(RandomizeSig2),    Randomize},
  162.            { RandomSig, sizeof(RandomSig),        Random},
  163.            { KeypressSig, sizeof(KeypressSig),    Keypress}
  164. };
  165.  
  166. #define NUM_ROUTINES (sizeof(Routines)/sizeof(Routines[0]))
  167.  
  168. static inline word SelfRelPtr(word Addr)
  169. {
  170. #ifdef WORD_MEMORY
  171.   return(Addr-MemRd(Addr)/2);
  172. #else
  173.   return(Addr-MemRd(Addr));
  174. #endif
  175. }
  176.  
  177. static inline word ProcBase(word JTab)
  178. {
  179.   PointerCheck(JTab);
  180.   return (SelfRelPtr(WordIndexed(JTab,-1)));
  181. }
  182.  
  183. void ProcessNative(word JTab)
  184. {
  185.   word EntryPoint=ProcBase(JTab);
  186.   int i;
  187.   for (i=0; i<NUM_ROUTINES; i++)
  188.     {
  189.       int j;
  190.       for (j=0; j<Routines[i].SigLen; j++)
  191.     if (MemRdByte(EntryPoint,j)!=Routines[i].Sig[j])
  192.       goto next;
  193.       Routines[i].Proc(EntryPoint);
  194.       return;
  195.     next:
  196.       ;
  197.     }
  198.  
  199. #ifdef XXX
  200.   TermClose();
  201.   for (i=0;WordIndexed(EntryPoint,i)<JTab; i++)
  202.     {
  203.       word w=MemRd(WordIndexed(EntryPoint,i));
  204.       printf(" 0x%02x, 0x%02x,", w&0xff, w>>8);
  205.       if ((i&3)==3)
  206.     printf("\n");
  207.     }
  208.   printf("\n");
  209.   panic("ProcessNative: unsupported native routine at 0x%04x", EntryPoint);
  210. #else
  211.   XeqError(XNOTIMP);
  212. #endif
  213. }
  214.  
  215.